package rbac_api

import (
	"fmt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"kardou/app/model"
)

type userStatusSaveReq struct {
	Id string `p:"id"`
}

type userStatusSaveRes struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (a *userApi) StatusSave(r *ghttp.Request) {

	var req *userStatusSaveReq
	if err := r.Parse(&req); err != nil {
		r.Response.WriteJsonExit(userStatusSaveRes{
			Code: 0,
			Msg:  err.Error(),
		})
	}
	var user *model.User
	err := g.DB().Model("user").Where("id=?", req.Id).Scan(&user)
	if err != nil {
		r.Response.WriteJsonExit(userStatusSaveRes{
			Code: 0,
		})
	}
	if user.Id == 0 {
		r.Response.WriteJsonExit(userStatusSaveRes{
			Code: 0,
			Msg:  "用户不存在",
		})
	}

	fmt.Println(user.Id)

	g.DB().Model("user").Where("id=?", req.Id).Data(map[string]int{
		"status": -user.Status,
	}).Update()
	r.Response.WriteJsonExit(userStatusSaveRes{
		Code: 1,
	})
}
